package daoimpl;

import dao.UserDao;
import javabean.User;
import util.JDBCUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * packageName com.mt.bms.daoimpl
 *
 * @author Administrator
 * @version 1.0
 * @className UserDaoImpl (此处以class为例)
 * @date 2024/8/2
 * @description
 */
public class UserDaoImpl implements UserDao<User> {
    @Override
    public int insert(User user) {
        String sql = "insert into user(username, password, name, email, balance) value(?, ?, ?, ?, ?)";
        return JDBCUtil.dml(sql, user.getUserName(), user.getPassWord(), user.getName(), user.getEmail(), user.getBalance());
    }

    @Override
    public ArrayList<User> selectByUsernameAndPassword(String username, String password) {
        String sql = "select * from user where username = ? and password = ?";
        return JDBCUtil.dql(sql, User.class, username, password);
    }

    @Override
    public int deleteById(int id) {
        String sql = "delete from user where id = ?";
        return JDBCUtil.dml(sql, id);
    }

    @Override
    public int updatePasswordById(int id, String password) {
        String sql = "update user set password = ? where id = ?";
        return JDBCUtil.dml(sql, password, id);
    }

    @Override
    public ArrayList<User> selectAll() {
        String sql = "select * from user";
        return JDBCUtil.dql(sql, User.class);
    }

    @Override
    public ArrayList<User> selectById(int id) {
        String sql = "select * from user where id = ?";
        return JDBCUtil.dql(sql, User.class, id);
    }

    @Override
    public ArrayList<User> selectByUserName(String userName) {
        String sql = "select * from user where username = ?";
        return JDBCUtil.dql(sql, User.class, userName);
    }

    @Override
    public int updateAddBalanceById(int id, double blance) {
        String sql = "update user set balance = balance + ? where id = ?";
        return JDBCUtil.dml(sql, blance, id);
    }

    @Override
    public int updateSubBalanceById(int id, double blance) {
        String sql = "update user set balance = balance - ? where id = ?";
        return JDBCUtil.dml(sql, blance, id);
    }

    @Override
    public int updateBalanceByMyIdAndToId(int myId, int toMyId, double balance) {
        String sql1 = "update user set balance = balance - ? where id = ?";
        String sql2 = "update user set balance = balance + ? where id = ?";
        Connection connection = JDBCUtil.getConnection();

        try {
            JDBCUtil.dml(connection, sql1, balance, myId);
            JDBCUtil.dml(connection, sql2, balance, toMyId);
            connection.commit();
            return 1;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException k) {
                k.printStackTrace();
            }
            return -1;
        } finally {
            JDBCUtil.close(connection);
        }
    }
}